package cn.toprog.viewer.component;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Callback;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

/**
 * 项目名称: TaoImagerViewer
 * 创建时间: 2021/4/7 16:48
 * 作者姓名: xiaolei
 */

public class ComponentFactory {
    public static TreeView<File> createFolderTree() {
        TreeView<File> folderTree = null;

        FolderTreeItem root = new FolderTreeItem(new File("ROOT"), true);
        root.loadChildren();

        folderTree = new TreeView<>(root);
        folderTree.setShowRoot(false);
        folderTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

        // 设置Tree节点的绘制方式
        folderTree.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
            @Override
            public TreeCell<File> call(TreeView<File> param) {
                return new TreeCell<File>() {
                    @Override
                    protected void updateItem(File item, boolean empty) {
                        super.updateItem(item, empty); // empty 表示有无数据与cell关联

                        if (empty) {
                            setText(null);
                            setGraphic(null);
                        } else {
                            FileSystemView fsv = FileSystemView.getFileSystemView();
                            String text = fsv.getSystemDisplayName(item);
                            java.awt.Image awtImage = ((ImageIcon) fsv.getSystemIcon(item)).getImage();
                            BufferedImage bi = null;
                            if (awtImage instanceof BufferedImage) {
                                bi = (BufferedImage) awtImage;
                            } else {
                                bi = new BufferedImage(awtImage.getWidth(null), awtImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
                                Graphics2D g2 = bi.createGraphics();
                                g2.drawImage(awtImage, 0, 0, null);
                                g2.dispose();
                            }
                            Image fxImage = SwingFXUtils.toFXImage(bi, null);
                            ImageView graphics = new ImageView(fxImage);

                            setText(text);
                            setGraphic(graphics);
                        }
                    }
                };
            }
        });

        folderTree.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == null) {
                System.out.println("----> Select Nothing!");
                return;
            }
            System.out.println("----> Select " + newValue.getValue());
        });


        // 加载根节点的第一层子节点的子节点
        for (TreeItem<File> item : root.getChildren()) {
            ((FolderTreeItem) item).loadChildren();
        }

        folderTree.getSelectionModel().select(root);
        return folderTree;
    }

}
